import io from '../../utils/weapp.socket.io.js'
Page({
  data: {
    isOwner: false,
    pocker: '',
    users: [],
    connectStatus: 0, // 0未连接服务器，1已连接服务器
    isShowWord: false,
  },
  hideWord() {
    this.setData({
      isShowWord: false
    })
  },
  showWord() {
    this.setData({
      isShowWord: true
    })
  },
  connect() {
    let that = this
    wx.connectSocket({
      url: 'wss://www.chunling.online:2900/'
    })
    wx.onSocketOpen(function (res) {
      that.setData({
        connectStatus: 1
      })
      console.log('websocket 已经连接服务器', res)
      that.send('createRoom', that.data.roomKey, that.data.user)
    })
  },
  close() {
    this.send('leaveRoom', this.data.roomKey, this.data.user)
    let that = this
    wx.closeSocket()
    wx.onSocketClose(function (res) {
      that.setData({
        connectStatus: 0
      })
      console.log('websocket服务器已经断开', res)
    })
  },
  send(eventName, roomKey, data) {
    let obj = {
      event: eventName, // 事件
      roomKey: roomKey, // 房间号
      data: data, // 传送数据
    }
    wx.sendSocketMessage({
      data: JSON.stringify(obj),
      success(res) {
        console.log('sendSocketMessage发送消息至服务器成功', res)
      },
      fail(err) {
        console.error('sendSocketMessage发送消息至服务器报错', err)
      }
    })
  },
  onReady() {
    console.log('onReady---------')
    let that = this
    that.setData({
      roomKey: wx.getStorageSync('roomKey'),
      user: wx.getStorageSync('userInfo')
    })
    this.connect()
    wx.setNavigationBarTitle({
      title: '发牌房间' + that.data.roomKey
    })
  },
  onLoad() {
    console.log('onLoad---------')
    let that = this
    let key = wx.getStorageSync('roomKey')
    wx.onSocketMessage(function (res) {
      console.log(res.data)
      let obj = JSON.parse(res.data)
      console.log('这是来自服务器的消息', obj.event, obj.roomKey, obj.data)
      if (obj.event == 'intoRoom' && key == obj.roomKey) {
        that.resetUsers(obj.data)
      }
      if (obj.event == 'leaveRoom' && key == obj.roomKey) {
        that.resetUsers(obj.data)
      }
      if (obj.event == 'revivePocker' && key == obj.roomKey) {
        that.deliverPocker(obj.data)
      }
    })
  },
  onUnload() {
    console.log('onUnload---------')
    this.close()
  },
  onHide() {
    
  },
  resetUsers(users) {
    this.setData({
      users: users,
      isOwner: false
    })
    for (let i = 0; i < users.length; i++) {
      if (users[i].isOwner && (users[i].nickName == this.data.user.nickName)) {
        this.setData({
          isOwner: true
        })
      }
    }
  },
  deliverPocker(users) {
    let myuser = wx.getStorageSync('userInfo')
    for (let i = 0; i < users.length; i++) {
      if (users[i].nickName == myuser.nickName) {
        this.setData({
          pocker: users[i].pocker
        })
        this.showWord()
        break;
      }
    }
  },
  startGame() { // 开始发牌
    // this.socket.emit('deliverPocker', wx.getStorageSync('roomKey'))
    this.send('deliverPocker', this.data.roomKey, null)
  },
})

/** 触发
 * createRoom 进入房间：房间号key，用户信息user
 * leaveRoom 离开房间：房间号key，用户信息user
 * deliverPocker 开始发牌：房间号key
 */
/** 监听
 * intoRoom[roomKey] 有人进入房间 users
 * leaveRoom[roomKey] 有人离开房间 users
 * revivePocker[roomKey] 接收牌面，根据nickName筛选显示自己的牌面
 */